package NG::Sys::Cached::List::Record;
use strict;
use warnings;
use utf8;

sub new {
	my $class = shift;
	#my $data = shift;       # 0
	#my $object = shift;     # 1
	#my $entry = shift;      # 2
	#my $list_extern = shift;# 3
	
	return bless \@_,$class;
}
sub update{
	my ($self,$key,$val) = @_;
	return $self->[1]->update_data($self,$key,$val);
}
sub set_data {
	my $self = shift;
	my $data;
	if(ref $_[0]){
		$data = shift;
	}elsif(@_ % 2){
		die "odd number param\n";
	}else{
		$data ={@_};
	}
	my $t = $self->[1]->begin_transaction;
	while(my($key,$val) = each %$data){
		$self->[1]->update_data($self,$key,$val);
	}
	if($t){
		$self->[1]->commit;
	}
}
sub entry {
	my $self = shift;
	return $self->[2] ||= $self->[0]{ $self->[1]{parent}{ekey} };
}
sub remove {
	my $self = shift;
	return $self->[1]->update_data($self,$self->[1]{parent}{ekey},0);
}
sub __extern {
	my ($self,$pos) = @_;
	return $self->[3][$pos] ||= $self->[1]{parent}{_extern}[$pos]{object}->get_obj($self->entry,$self->[1]{readonly});
}
sub data {
	my $self = shift;
	return $self->[0];
}
our $AUTOLOAD;
sub DESTROY{}
sub AUTOLOAD {
	my $self = shift;
	(my $key = $AUTOLOAD) =~ s/.*://;

	if(defined(my $pos = $self->[1]{parent}{hash_extern}{$key})){
		return  $self->__extern($pos);
	}

	unless(exists $self->[0]{$key}){
		die "invalid key '$key' accesed\n";
	}

	if(@_){
		return $self->[1]->update_data($self,$key,@_);
	}else{
		return $self->[0]{$key};
	}
}

1;
